/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ResultEditor.Annotations;

import ResultEditor.ComplexRelationship.iListable;
import Relationship.Simple.DataTypes.Attribute;
import java.util.ArrayList;
import java.util.Vector;

/**
 *
 * @author Chris
 */
/**normal relationship of this annotation, normal is compare to complex relationship. */
public class NormalRelationship implements iListable
{
    /**
     * The name of the relationship
     */
    public String relationshipName;
    /**
     * The values that this relationship currently has
     */
    public Vector<String> relationshipValue = new Vector<String>();
    /**
     * The currently selected string in the List of Allowable Strings from
     * the Simple Schema.
     */
    private String currentlySelected = "";
    /**
     * If this is a repeat of a former attribute
     */
    public boolean deadName = false;
    /**
     * Default constructor
     */
    public NormalRelationship(){};
    /**
     * Constructor
     * @param name - the name of the relationship
     */
    public NormalRelationship(String name)
    {
        relationshipName = name;
        ArrayList<String> allowed = env.Parameters.SimpleRelationshipNames.getAllowed(name);
        if(allowed.size() != 0)
            currentlySelected = allowed.get(0);
    }
    /**
     * Constructor
     * @param name - name of the relationship
     * @param value - value of the relationship
     */
    public NormalRelationship(String name, String value)
    {
        relationshipName = name;
        relationshipValue.add(value);
    }
    /**
     * Copy this object.
     * @return - a copy of this object that doesn't share any references.
     */
    public NormalRelationship copy()
    {
        NormalRelationship toReturn = new NormalRelationship();
        toReturn.relationshipName = relationshipName;
        for(String s: relationshipValue)
            toReturn.relationshipValue.add(s);
        return toReturn;

    }
    /**
     * Get a copy of this object that can be written back to an Annotation.  This will
     * be called after the Annotation Attributes have been edited so it can be written
     * back to the annotation.
     * @return - A normal relationship ready to be written to an Annotation
     */
    public Object getWriteForm()
    {
        relationshipValue = new Vector<String>();
        relationshipValue.add(currentlySelected);
        return this;
    }
    /**
     * The currently selected String(used for columns in a JTable).
     * @param which - the column
     * @return - the currently selected item
     */
    public String getSelectedItem(int which)
    {
        if(currentlySelected.equals("") && relationshipValue.size() != 0)
            currentlySelected = relationshipValue.get(0);
        if (which == 0)
        {
            return relationshipName;
        }
        return currentlySelected;
    }
    /**
     * Set the string at a given column.(Used for editing within a JTable)
     * @param which - the column
     * @param value - the new value
     */
    public void setString(int which, String value)
    {
        if (which == 0)
        {
            if(!relationshipName.equals(value))
            {
                ArrayList<String> test = env.Parameters.SimpleRelationshipNames.getAllowed(value);
                if(test.size() != 0)
                {
                    currentlySelected = test.get(0);
                    relationshipValue = new Vector<String>();
                    relationshipValue.add(test.get(0));
                }
            }
            relationshipName = value;
        }
        if (which == 1)
        {
            value = value.replace("<html><font color = \"red\">", "");
            value = value.replace("</font></html>", "");
            currentlySelected = value;
        }
    }
    /**
     * Get the Allowed Strings for a given column.
     * @param which - the column.
     * @return - Return any Strings which can be in a given column.  If needsCombo(which)
     * returns false then only one String will be returned.
     */
    
    public Vector<String> getStrings(int which)
    {
        Vector<String> toReturn = new Vector<String>();

        if (which == 0)
        {
            boolean returnAll = false;
            for (Attribute att : env.Parameters.SimpleRelationshipNames.getAttributes())
            {
                if (att.getName().equals(relationshipName))
                {
                    returnAll = true;
                }

            }
            if(returnAll)
            {
                for(Attribute att: env.Parameters.SimpleRelationshipNames.getAttributes())
                    toReturn.add(att.getName());
            }
            else
            {
                toReturn.add(relationshipName);
            }
            return toReturn;
        }
        for (Attribute att : env.Parameters.SimpleRelationshipNames.getAttributes())
        {
            if (att.getName().equals(relationshipName))
            {
                for(String s: relationshipValue)
                {
                    if(!att.getAllowedEntries().contains(s))
                    {
                        toReturn.add("<html><font color = \"red\">" + s + "</font></html>");
                    }
                }
                for (String allowed : att.getAllowedEntries())
                {
                    toReturn.add(allowed);
                }
                
                return toReturn;
            }

        }
        return toReturn;

    }
    /**
     * Used for editing within a JTable, If the given column needs a combo box
     * then return true, otherwise return false.
     * @param which- the column
     * @return  - true if needs a combo box, false otherwise.
     */
    public boolean needsCombo(int which)
    {
        for(Attribute att: env.Parameters.SimpleRelationshipNames.getAttributes())
        {
            if(att.getName().equals(relationshipName))
                return true;
        }
        return false;
    }
    /**
     * 
     * @param which
     * @return
     */
    public boolean isModifiable(int which)
    {
        if(needsCombo(which))
            return true;
        return false;
    }
    public boolean hasEntry(int which)
    {
        if(which >1 || which< 0)
            return false;
        return true;
    }
    public boolean couldDelete()
    {
        return true;
    }

}
